Previous | Next | Trail Map | Creating a User Interface | Anatomy of a Program with a Graphical UI


Drawing

When a Java program with a Graphical UI needs to draw itself -- whether for the first time, or in response to becoming unhidden [wc?], or because its appearance needs to change to reflect something happening inside the program -- it starts with the top Component in the hierarchy and works its way down to the bottom-most Components. [true? couldn't you draw a subset of the hierarchy?] This is all orchestrated by the AWT drawing system.

When the Converter application draws itself, here's what happens:

  1. The Frame draws itself.
  2. The Converter object draws itself, drawing a box around its area.
  3. One of the two ConversionPanels draws itself, drawing a box around its area.
    Note: You can't rely on the order that two Components at the same level will be drawn in. For example, you can't rely on the metric panel being drawn before the U.S. one. Similarly, you can't rely on the order of drawing two Components at different levels if the lower Component isn't contained in the higher Component. [I just guessed this stuff. Make sure it's correct!]
  4. The contents of the ConversionPanel -- the Label, Panel, and Choice -- draw themselves.
In this way, each Component draws itself before any of the Components it contains. This ensures that a Panel's background, for example, is visible only where it isn't covered by one of the Components it contains.

How Drawing Requests Occur

Programs can draw only when the AWT tells them to. The reason is that each occurrence of a Component drawing itself must execute without interruption. Otherwise, unpredictable results could occur, such as a Button being drawn halfway, and then being interrupted by some lengthy animation. The AWT orders drawing requests by making them run in a single thread. A program can, however, request that the AWT schedule them for drawing.

The AWT requests that a Component draw itself by invoking the Component's update() method. The Component implementation of the update() method simply clears the Component's background (drawing a rectangle over the whole Component in the Component's background color) and then calling the Component's paint() method. The default implementation of the paint() method does nothing.

The Graphics Object

The only argument to the paint() and update() methods is a Graphics object that represents the context in which the Component can perform its drawing. Each Graphics object includes the following information: The Graphics class provides methods for the following:

How to Draw

The simplest way for a Component to draw itself is to put drawing code in its paint() method. This means that when the AWT makes a drawing request (by calling the Component's update() method, which is implemented as described above), the Component's entire area is cleared and then its paint() method is called. For programs that don't repaint themselves often, the performance of this scheme is fine.

Below is an example of an implementation of the paint() method. Both the Converter and ConversionPanel classes draw a box around their area using this code. (Both classes also implement an insets() method that specifies the padding around the panel's contents. If they didn't have this method, the box drawn in the paint() method would overlap the external boundaries of the panel's contents.)

public void paint(Graphics g) {
    Dimension d = size();
    g.drawRect(0,0, d.width - 1, d.height - 1);
}
Programs that repaint themselves often can use two techniques to improve their performance: implementing both update() and paint() and using double-buffering. These techniques are discussed [nowhere yet].


Previous | Next | Trail Map | Creating a User Interface | Anatomy of a Program with a Graphical UI